﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BL;
using DB;
using DB;

namespace UI
{
    public partial class SubjectItemsForm : Form
    {
        
        SubjectItem subject;
        SubjectItem[] list;
        SubjectItemMapper mapper = new SubjectItemMapper();
        DataTable dt;
        public SubjectItemsForm()
        {
            InitializeComponent();
           
            dt = DB.AccessDb.Instance.GetTable("SubjectItem");
            
            subjectsdataGridView.AllowDrop = true;
            subjectsdataGridView.AllowUserToAddRows = true;
            subjectsdataGridView.AllowUserToDeleteRows = true;

            this.subjectsdataGridView.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.subjectsdataGridView_CellValueChanged);
        }

       
        private void SubjectItemsForm_Load(object sender, EventArgs e)
        {
            list = Subjects.Instance.Items;
            foreach (SubjectItem item in list)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.Cells.Add(new DataGridViewTextBoxCell());
                row.Cells.Add(new DataGridViewTextBoxCell());
                row.Cells.Add(new DataGridViewTextBoxCell());
                DataGridViewComboBoxCell cmb = new DataGridViewComboBoxCell();
                cmb.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                cmb.Items.Add(LessonTypes.Лекции.ToString());
                cmb.Items.Add(LessonTypes.Практика.ToString());
                cmb.Items.Add(LessonTypes.Курсовое.ToString());
                cmb.Items.Add(LessonTypes.Дипломное.ToString());
                row.Cells.Add(cmb);
                row.Cells.Add(new DataGridViewTextBoxCell());
                row.Cells[0].Value = item.ID;
                row.Cells[1].Value = item.Group;
                row.Cells[2].Value = item.Subject;
                row.Cells[3].Value = item.LessonType.ToString();
                row.Cells[4].Value = item.Hours;
                subjectsdataGridView.Rows.Add(row);
                //listItems.Add(item);
            }
        }             

        
        

        private void copyBtn_Click_1(object sender, EventArgs e)
        {
            int pos = subjectsdataGridView.SelectedRows[0].Index;
            try
            {
                subject = new SubjectItem();
                subject.ID = Subjects.Instance.FreeID;
                subject.Group = subjectsdataGridView.Rows[pos].Cells[1].Value.ToString();
                subject.Subject = subjectsdataGridView.Rows[pos].Cells[2].Value.ToString();

                subject.LessonType = LessonTypeConverter.Convert(subjectsdataGridView.Rows[pos].Cells[3].Value.ToString());
                subject.Hours = Convert.ToInt32(subjectsdataGridView.Rows[pos].Cells[4].Value);
            }
            catch
            {
                
            }
        }

        private void pasteBtn_Click_1(object sender, EventArgs e)
        {
            try
            {
                DataGridViewRow row = new DataGridViewRow();
                row.Cells.Add(new DataGridViewTextBoxCell());
                row.Cells.Add(new DataGridViewTextBoxCell());
                row.Cells.Add(new DataGridViewTextBoxCell());
                DataGridViewComboBoxCell cmb = new DataGridViewComboBoxCell();
                cmb.Items.Add(LessonTypes.Лекции.ToString());
                cmb.Items.Add(LessonTypes.Практика.ToString());
                cmb.Items.Add(LessonTypes.Курсовое.ToString());
                cmb.Items.Add(LessonTypes.Дипломное.ToString());
                row.Cells.Add(cmb);
                row.Cells.Add(new DataGridViewTextBoxCell());
                row.Cells[0].Value = subject.ID;
                row.Cells[1].Value = subject.Group;
                row.Cells[2].Value = subject.Subject;
                row.Cells[3].Value = subject.LessonType.ToString();
                row.Cells[4].Value = subject.Hours;
                this.subjectsdataGridView.RowsAdded += new System.Windows.Forms.DataGridViewRowsAddedEventHandler(this.subjectsdataGridView_RowsAdded);
                subjectsdataGridView.Rows.Add(row);
                
                

                //subjectsdataGridView.Rows.Add(new string[] { null, subject.Group, subject.Subject, subject.LessonType.ToString(), subject.Hours.ToString() });
            }
            catch
            {
                
            }
        }

        

        private void linkLabel1_LinkClicked_1(object sender, LinkLabelLinkClickedEventArgs e)
        {
            LessonsForm empForm = new LessonsForm();
            empForm.Show();
        }

        private void subjectsdataGridView_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            Column3.Items.Add(LessonTypes.Лекции.ToString());
            Column3.Items.Add(LessonTypes.Практика.ToString());
            Column3.Items.Add(LessonTypes.Курсовое.ToString());
            Column3.Items.Add(LessonTypes.Дипломное.ToString());
            SubjectItem si = new SubjectItem();
            si.ID = Subjects.Instance.FreeID;

            Subjects.Instance.Add(si);
            SubjectItemMapper tMapper = new SubjectItemMapper();
            tMapper.Insert(si);
            subjectsdataGridView.Rows[e.Row.Index - 1].Cells[0].Value = si.ID.ToString(); ;
            AccessDb.Instance.Commit();
        }

        private void subjectsdataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            Int64 id = Int64.Parse(subjectsdataGridView.Rows[e.RowIndex].Cells[0].Value.ToString());
            SubjectItem si = new SubjectItem();
            si.ID = id;

            if (subjectsdataGridView.Rows[e.RowIndex].Cells[1].Value != null)
                si.Group = subjectsdataGridView.Rows[e.RowIndex].Cells[1].Value.ToString();
            if (subjectsdataGridView.Rows[e.RowIndex].Cells[2].Value != null)
                si.Subject = subjectsdataGridView.Rows[e.RowIndex].Cells[2].Value.ToString();
            if (subjectsdataGridView.Rows[e.RowIndex].Cells[3].Value != null)
                si.LessonType = LessonTypeConverter.Convert(subjectsdataGridView.Rows[e.RowIndex].Cells[3].Value.ToString());
            if (subjectsdataGridView.Rows[e.RowIndex].Cells[4].Value != null)
                si.Hours = int.Parse(subjectsdataGridView.Rows[e.RowIndex].Cells[4].Value.ToString());
            Subjects.Instance[id] = si;
            SubjectItemMapper tMapper = new SubjectItemMapper();
            tMapper.UpdateEntity(si);
            AccessDb.Instance.Commit();
        }

        private void subjectsdataGridView_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
           SubjectItem si = Subjects.Instance[long.Parse(e.Row.Cells[0].Value.ToString())];

           SubjectItemMapper tMapper = new SubjectItemMapper();
           tMapper.DeleteEntity(si);

           Subjects.Instance.Delete(si.ID);
           AccessDb.Instance.Commit();
        }

        private void subjectsdataGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            if ((e.RowIndex > (list.Length - 1)) && (subjectsdataGridView.Rows[e.RowIndex].Cells[0].Value!=null))
            {
                SubjectItem si = new SubjectItem();
                si.ID = Subjects.Instance.FreeID;
                si.Group = subjectsdataGridView.Rows[e.RowIndex].Cells[1].Value.ToString();
                si.Subject = subjectsdataGridView.Rows[e.RowIndex].Cells[2].Value.ToString();
                si.LessonType = LessonTypeConverter.Convert(subjectsdataGridView.Rows[e.RowIndex].Cells[3].Value.ToString());
                si.Hours = int.Parse(subjectsdataGridView.Rows[e.RowIndex].Cells[4].Value.ToString());

                Subjects.Instance.Add(si);
                SubjectItemMapper tMapper = new SubjectItemMapper();

                subjectsdataGridView.Rows[e.RowIndex - 1].Cells[0].Value = si.ID.ToString(); ;
                tMapper.Insert(si);
                AccessDb.Instance.Commit();
            }
        }
    }
}